var app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        id: '',
        loadStatus: false, //加载状态
        detail: {}, //数据
        showShareType: false, //显示分享类型
        shareConfig: false, //h5是否配置了分享信息
        addShareStatus: false, //是否记录了分享记录
        fromUid: '', //推荐人
        shareList: '', //分享排行
        userId: '',
        content: '',
        clickId: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        // options.id = 1;

        console.log(options)


        // url参数中可以获取到gdt_vid、weixinadinfo参数值 let gdt_vid = options.gdt_vid
        let weixinadinfo = options.weixinadinfo
        // 获取广告id
        let aid = 0
        if (weixinadinfo) {
            let weixinadinfoArr = weixinadinfo.split('.');
            this.setData({
                clickId: weixinadinfoArr[0]
            })
        }


        this.setData({
            id: options.id
        })

        wx.showLoading();
        this.get_detail(() => {
            this.setData({
                loadStatus: true
            })
            wx.hideLoading();
        })

        let userInfo = wx.getStorageSync('userinfo');
        this.userId = userInfo ? userInfo.loginUid : '';
        if (options.uid && userInfo) {
            this.setData({
                fromUid: options.uid
            })
            this.add_share();
        }



        this.get_share_list();
    },



    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {
        let userInfo = wx.getStorageSync('userinfo');
        return {
            title: this.data.detail.name,
            imageUrl: this.data.detail.cover_image,
            path: 'pages/activity/enrollment/index?id=' + this.data.detail.id + '&uid=' + userInfo.loginUid
        }
    },
    to_poster() {
        let userInfo = wx.getStorageSync('userinfo');
        let url = 'pages/activity/enrollment/index?id=' + this.data.detail.id + '&uid=' + userInfo.loginUid;

        let shareInfo = {
            title: this.data.detail.name,
            image: this.data.detail.cover_image,
            path: url
        }

        wx.setStorageSync('shareInfo', shareInfo);
        wx.navigateTo({
            url: '/pages/common/poster/index'
        })
    },
    show_share_type() {
        this.setData({
            showShareType: true
        })
    },
    hide_share_type() {
        this.setData({
            showShareType: false
        })
    },
    buy() {
        let that = this;
        

        let postData = {
            activity_id: this.data.detail.id,
            type: 6,
            number: 1,
            gold: 0,
            score: 0,
            click_id: this.data.clickId
        };

        app.request({
            url: 'order/pay',
            data: postData,
            sCallback: (res) => {

                if (res.code == 1) {
                    wx.requestPayment({
                        'timeStamp': res.data.timeStamp,
                        'nonceStr': res.data.nonceStr,
                        'package': res.data.package,
                        'signType': res.data.signType,
                        'paySign': res.data.paySign,
                        'signType': res.data.signType,
                        success(res) {
                            that.up_ad();
                            wx.navigateTo({
                                url: '/pages/activity/select-school/index?id=' + that.data.detail.id
                            })
                        }
                    })
                } else if (res.code == 2) {
                    that.up_ad();
                    wx.navigateTo({
                        url: '/pages/activity/select-school/index?id=' + that.detail.id
                    })
                } else {
                    app.alert(res.msg);
                }
            }
        })

    },
    //获取详情信息
    get_detail(callback) {
        let that = this;
        app.request({
            url: 'enrollment/detail',
            data: {
                id: that.data.id
            },
            sCallback: (res) => {
                if (res.code == 1) {
                    that.detail = res.data;
                    let content = '';
                    if (res.data.content) {
                        content = res.data.content.replace(/\\/g, "").replace(/<img/g, "<img style=\"max-width:100%;display:inline-block;\"");
                    }
                    that.setData({
                        detail: res.data,
                        content: content
                    })


                    callback && callback();
                } else {
                    app.alert(res.msg, () => {
                        pages = getCurrentPages();
                        if (pages.length > 1) {
                            wx.navigateBack();
                        } else {
                            wx.navigateTo({
                                url: '/pages/index/index'
                            })
                        }
                    })
                }
            }
        })
    },
    //添加分享记录
    add_share() {
        let that = this;
        if (that.data.addShareStatus == true || !that.data.fromUid) {
            return false;
        }

        app.request({
            url: 'group/share',
            data: {
                id: that.data.id,
                user_id: that.data.fromUid,
                type: 2
            },
            sCallback: (res) => {
                console.log(res);
                if (res.code == 1) {
                    that.setData({
                        addShareStatus: true
                    })
                }

            }
        })
    },
    //获取分享排行榜
    get_share_list() {
        let that = this;
        app.request({
            url: 'group/shareCount',
            data: {
                id: that.data.id,
                type: 2
            },
            sCallback: (res) => {
                if (res.code == 1) {
                    that.setData({
                        shareList: res.data
                    })
                }
            }
        })
    },
    //上报腾讯广告
    up_ad() {
        let that = this;
        if (that.data.clickId) {
            app.request({
                url: 'ad/upAd',
                data: {
                    click_id: that.data.clickId
                },
                sCallback: (res) => {
                    console.log(res)
                }
            })
        }
    }
})